<template>
  <div>
    <div class="home-container">
      <!-- 导航栏 -->
      <van-nav-bar class="page-nav-bar" fixed>
        <van-button slot="title" 
        class="search-btn"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button>
      </van-nav-bar>

      <!-- 频道列表 -->
      <!-- 
        v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签。
        animated 属性可以开启切换标签内容时的转场动画。
        swipeable 属性可以开启滑动切换标签页。
       -->
      <van-tabs class="channel-tabs" v-model="active" animated swipeable>
        <van-tab :title="item.name"
          v-for="item in channels"
        :key="item.id"
        >
        <!-- 文章列表 -->
        <ArticleList :channels="item"/>
        </van-tab>
        <!-- 右侧更多 -->
        <div slot="nav-right" class="placeholder"></div>
        <div slot="nav-right" class="hambuerger-btn" @click="(isEditeShow = true)">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </van-tabs>

      <!-- 频道编辑弹出 -->
      <van-popup v-model="isEditeShow" 
          closeable
          position="bottom" 
          close-icon-position="top-left"
          :style="{ height: '100%' }"
      >
      <ChannelEdit :Mychannels = "channels" 
        :active = "active"
        @update-active = 'onupdateActive'
      ></ChannelEdit>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { getUseChannels } from '@/api/user'
import ArticleList from '../home/components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
    name:'HomeIndex',
    data() {
      return {
        active: 0,
        channels:[], //频道列表
        isEditeShow: false, //弹出层显示状态
      }
    },
    created(){
      this.loadChannels()
    },
    methods:{
      async loadChannels(){
        try{
          // const {data} = await getUseChannels()
          // this.channels = data.data.channels

          let channelsZ = [] //中间频道存储变量--中转站
          if(this.user){
             //已登录，请求获取用户频道列表
            const {data} = await getUseChannels()
            channelsZ = data.data.channels
          }else{
             //未登录，判断是否有本地频道列表数据
             const localChannels = getItem('TOUTIAO_CHANNELS')
                if(localChannels){
                  //  有，则本地获取使用
                  channelsZ = localChannels
                }else{
                   //  没有，则请求获取默认频道列表
                   const {data} = await getUseChannels()
                   channelsZ = data.data.channels
                }
          }
          this.channels = channelsZ
         
               
        }catch(err){
          this.$toast('获取频道列表信息失败',err)
        }
      },
      onupdateActive(el,isEditeShow = true){
        console.log('hoem'+el);
        //更新激活的频道项
        this.active = el
        //关闭编辑频道弹窗
        this.isEditeShow = isEditeShow
      }
    },
    components:{
      ArticleList,
      ChannelEdit
    },
    computed:{
      ...mapState(['user'])
    }
}
</script>

<style lang="less" scoped>
  .home-container{
    padding-top: 174px;
    padding-bottom: 100px;
    /deep/.van-nav-bar__title{
      max-width: unset;
    }
    .search-btn{
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      border: none;
      font-size: 28px;
      .van-icon{
        font-size: 32px;
      }
    }
    /deep/.channel-tabs{
      .van-tabs__content{
        min-height: 79vh;
      }
      .van-tabs__wrap{
        height: 82px;
        position: fixed;
        left: 0;
        right: 0;
        top: 92px;
        z-index: 1;
      }
      .van-tab{
        min-width: 200px;
        border-right: 1px solid #edeff3;
        .van-tab__text{
          font-size: 30px;
          color: #777777;
        }
      }
      .van-tab--active{
        color: #333333;
      }
      .van-tabs__nav{
        padding-bottom: 0;
      }
      .van-tabs__line{
        bottom: 8px;
        width: 31px !important;
        height: 6px;
        background-color: #3296fa;
      }
      .placeholder{
        width: 66px;
        height: 82px;
        flex-shrink: 0;
      }
      .hambuerger-btn{
        position: fixed;
        right: 0;
        width: 66px;
        height: 82px;
        display: flex;
        justify-items: center;
        align-items: center;
        background-color: #fff;
        opacity: 0.902;
        i.toutiao{
          font-size: 33px;
        }
        &::before{
          content: '';
          position: absolute;
          left: 0;
          width: 1px;
          height: 100%;
          background-image: url(../../assets/gradient-gray-line.png);
          background-size: contain;
        }
      }
    }
  }

</style>